package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrain;
import com.system.business.domain.Train;
import com.system.business.req.DailyTrainQueryReq;
import com.system.business.req.DailyTrainSaveReq;
import com.system.business.resp.DailyTrainQueryResp;
import com.system.business.service.*;
import com.system.business.mapper.DailyTrainMapper;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Admin
* @description 针对表【daily_train(每日车次)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:15
*/
@Service
public class DailyTrainServiceImpl extends ServiceImpl<DailyTrainMapper, DailyTrain>
    implements DailyTrainService{
    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainService.class);

    @Autowired
    private DailyTrainMapper dailyTrainMapper;

    @Autowired
    private TrainService trainService;

    @Resource
    private DailyTrainStationService dailyTrainStationService;

    @Resource
    private DailyTrainCarriageService dailyTrainCarriageService;

    @Resource
    private DailyTrainSeatService dailyTrainSeatService;

    @Resource
    private DailyTrainTicketService dailyTrainTicketService;
    @Override
    public PageResp<DailyTrainQueryResp> queryList(DailyTrainQueryReq req) {
        Page<DailyTrain> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<DailyTrain> passengerQueryWrapper = new QueryWrapper<>();
        Page<DailyTrain> pageData = dailyTrainMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<DailyTrainQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), DailyTrainQueryResp.class);

        PageResp<DailyTrainQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }


    @Override
    public void save(DailyTrainSaveReq req) {
        DateTime now = DateTime.now();

        DailyTrain dailyTrain = BeanUtil.copyProperties(req, DailyTrain.class);
        if (ObjectUtil.isNull(dailyTrain.getId())) {
            dailyTrain.setId(SnowUtil.getSnowflakeNextId());
            dailyTrain.setCreateTime(now);
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.insert(dailyTrain);
        } else {
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.updateById(dailyTrain);
        }
    }

    /*
     * 生成某日所有车次
     */
    @Override
    public void genDaily(Date date) {
        List<Train> trainList = trainService.list();
        if (CollectionHelper.isEmpty(trainList)){
           return;
        }
        for (Train train : trainList) {
            genDailyTrain(date, train);
        }

    }


    @Transactional
    public void genDailyTrain(Date date, Train train) {
        LOG.info("生成日期【{}】车次【{}】的信息开始", DateUtil.formatDate(date), train.getCode());
        // 删除该车次已有的数据
        QueryWrapper<DailyTrain> dailyTrainQueryWrapper = new QueryWrapper<>();
        dailyTrainQueryWrapper.eq("date",date).eq("code",train.getCode());
        dailyTrainMapper.delete(dailyTrainQueryWrapper);

        // 生成该车次的数据
        DateTime now = DateTime.now();
        DailyTrain dailyTrain = BeanUtil.copyProperties(train, DailyTrain.class);
        dailyTrain.setId(SnowUtil.getSnowflakeNextId());
        dailyTrain.setCreateTime(now);
        dailyTrain.setUpdateTime(now);
        dailyTrain.setDate(date);
        dailyTrainMapper.insert(dailyTrain);

        // 生成该车次的车站数据
        dailyTrainStationService.genDaily(date, train.getCode());

        // 生成该车次的车厢数据
        dailyTrainCarriageService.genDaily(date, train.getCode());

        // 生成该车次的座位数据
        dailyTrainSeatService.genDaily(date, train.getCode());

        // 生成该车次的余票数据
        dailyTrainTicketService.genDaily(dailyTrain, date, train.getCode());

    }
}




